package org.flash.display{
	import org.flash.net.URLLoaderChild;
	import org.flash.text.TextFieldFormat;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.net.URLLoader;

	public class LoaderList {
		private var _xml:XML;
		private var _index:int;
		private var _txt:TextFieldFormat;
		private var _loader:LoaderChild;
		private var _containter:DisplayObjectContainer;

		public function LoaderList(url:String, target:DisplayObjectContainer = null){
			this._containter = target;
			var urlloader:URLLoader = new URLLoaderChild(url, xmlComplete);
		}

		/**
		 * xml数据
		 * 创建文本显示加载进度
		 * @param urlloader
		 * 
		 */		
		private function xmlComplete(urlloader:URLLoaderChild):void {
			var filter:GlowFilter = null;
			var arr:Array = null;
			
			_xml = new XML(urlloader.data);
			if (_containter != null){
				_txt = new TextFieldFormat();
				_txt.mouseEnabled = false;
				_txt.width = 200;
				_containter.addChild(_txt);
				filter = new GlowFilter(16777215, 1, 3, 3, 8, 3);
				arr = new Array(filter);
				_txt.filters = arr;
			}
			LoadChildInfo.loaderList = this;
			if (LoadChildInfo.mainLoaderStatus == false){
				this.start();
			}
		}

		/**开始加载xml内容*/
		public function start():void {
			if (_xml != null && _index < _xml.item.length()){
				_loader = new LoaderChild(String(_xml.item[_index]), complete, progress, history, true);
			}
		}

		/**加载进度*/
		private function progress(num:Number):void {
			if (this._containter != null){
				_txt.text = Math.floor(num) + "%" + ": " + String(_index + 1) + "/" + _xml.item.length();
			}
		}
		
		/**当前加载完成*/
		private function complete(loader:LoaderChild):void {
			_index++;
			this.stop();
			this.start();
		}
		
		public function history(bool:Boolean):void {
			trace(bool);
		}
		
		/**销毁当前对象*/
		public function stop():void {
			if (_loader != null){
				_loader.unloadAndStop();
				_loader = null;
				Config.depose();
			}
		}
	}
}